package com.example.controller;


import com.example.pojo.Customer;
import com.example.service.CustomerService;
import com.example.tools.JwtUtils;
import com.example.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private CustomerService CustomerService;

    @PostMapping("/login")
    public Result login(@RequestBody Customer customer) {
        log.info("员工登录：{}", customer);
        Customer e = CustomerService.login(customer);
        //登录成功，生成令牌，下发令牌
        if (e != null) {
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("cusCode", e.getCusCode());
            String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }

        //登录失败，返回错误信息

        return Result.error("用户名或密码错误");
    }


}
